(function($){
   var socket = io.connect();
   var msgtpl = $('#msgtpl').html();

    $('#loginform').submit(function(event){
        console.log('okk');
        event.preventDefault();

        socket.emit('login', {
            password : $('#passwordL').val(),
            mail     : $('#mailL').val()
        });
    });

    $('#registrationform').submit(function(event){
        console.log('register');
        event.preventDefault();

        if($('#adminR').val() == "oui"){
            socket.emit('registration', {
                mail           : $('#mailR').val(),
                password       : $('#passwordR').val(),
                passwordverif  : $('#passwordV').val(),
                admin          : "oui"
            });
        }else{
            socket.emit('registration', {
               mail           : $('#mailR').val(),
               password       : $('#passwordR').val(),
               passwordverif  : $('#passwordV').val()
            });
        }
    });

    $('#home').click(function(){
        window.location = '/';
    });

    socket.on('newGame', function(){
        window.location = '/difficulty';
    });

    socket.on('logged', function(){
       window.location = '/map';
    });

    socket.on('mailUsed', function(){
       $('#mailR').val('');
       $('#passwordR').val('');
       $('#passwordV').val('');
       alert('Mail already used');
    });

    socket.on('newGame', function(){
       window.location = '/difficulty';
    });
    socket.on('wrongLogin', function(){
        $('#mailL').val('');
        $('#passwordL').val('');
        alert('Wrong password or mail');
    });

    socket.on('registerNull', function(){
        $('#mailR').val('');
        $('#passwordR').val('');
        $('#passwordV').val('');
        alert('Veuillez remplir tout les champs');
    });

    socket.on('mdpIdentique', function(){
        $('#mailR').val('');
        $('#passwordR').val('');
        $('#passwordV').val('');
        alert('Les mots de passes ne correspondent pas');
    });

    socket.on('registerSuccess', function(){
        window.alert("L'utilisateur a été enregistré");
    })

    $('#logOut').click(function(){
        window.location = '/';
        socket.emit('disconnected');
    });

})(jQuery);